package cn.gqr.controller;

import cn.gqr.entity.Cart;
import cn.gqr.entity.CartOrder;
import cn.gqr.entity.OrderDetail;
import cn.gqr.service.CartService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
;
import java.util.Map;

@RestController
@RequestMapping("/api/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    //购物车转订单
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/placingAnOrder")
    public Map<String,Object> placingAnOrder(@RequestBody CartOrder cartOrder){
        String s = cartService.placingAnOrder(cartOrder);
        return RespJson.pack(200,"订单id",s);
    }

    //添加购物车
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/addShoppingCart")
    public Map<String,Object> addShoppingCart(@RequestBody Cart cart){
        boolean b = cartService.addShoppingCart(cart);
        return RespJson.pack(200,b?"添加成功":"添加失败",null);
    }

    //删除购物车
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/delCart/{id}")
    public Map<String,Object> removeShoppingCartById(@PathVariable("id") Integer id){
        boolean b = cartService.removeShoppingCartById(id);
        return RespJson.pack(200,b?"删除成功":"删除失败",null);
    }

    //查询购物车分页
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/selectCarts/{start}/{num}")
    public Map<String,Object> selectCarts(@PathVariable("start") int num, @PathVariable("num") int start){
        Map<String, Object> map = cartService.selectCarts(start, num);
        return RespJson.pack(200,"购物车信息",map);
    }

}
